'use strict';

import request from 'request';

export default class extends think.controller.base {
  /**
   * some base method in here
   */
  async __before() {

    //判断登录
    let userInfo = await this.session('userInfo');

    let log = {};
    log.id = null;
    log.type - 'visit';
    log.ipAddress = this.ip();
    log.userAgent = this.userAgent();
    log.url = this.http.url;
    log.createTime = think.datetime();

    //写入日志
    request.post('http://127.0.0.1:8360/api/log/add', {
      form: {
        log: JSON.stringify(log)
      }
    }, function(err, httpResponse, body) {

      if (err) {
        think.error(err);
      } else {
        think.log(body);
      }
    });

    if (think.isEmpty(userInfo)) {
      return this.redirect('/login');
    } else {
      this.assign('userInfo', userInfo);
    }
  }
}